package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.AddressBookDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderDetailDao;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.AddressBook;
import com.itheima.pojo.OrderDetail;
import com.itheima.pojo.Orders;
import com.itheima.pojo.ShoppingCart;
import com.itheima.pojo.User;
import com.itheima.service.OrderService;
import com.itheima.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, Orders>  implements OrderService {

    @Autowired
    ShoppingCartDao shoppingCartDao;

    @Autowired
    UserDao userDao;

    @Autowired
    AddressBookDao addressBookDao;

    @Autowired
    OrderDetailDao orderDetailDao;

    @Autowired
    OrderDao orderDao;
    @Override
    public String submitOrders(Orders orders) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //1.查看购物车数据
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartDao.selectList(qw);
        //2.查看用户数据
        User user = userDao.selectById(userId);
        //3.查看收货地址
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookDao.selectById(addressBookId);
        //生成订单号
        Long orderNumber = IdWorker.getId();
        //总价初始值
        BigDecimal amount = new BigDecimal("0");

        // 4、保存订单明细数据：点了哪些菜品或套餐
        for (ShoppingCart shop : shoppingCartList) {
            //订单详情实体类
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shop, orderDetail);
            //手动赋值订单id
            orderDetail.setOrderId(orderNumber);
            //计算价格 数量 * 单价
            BigDecimal price = shop.getAmount();
            BigDecimal number = BigDecimal.valueOf(shop.getNumber());
            orderDetail.setAmount(price.multiply(number));
            amount = amount.add(orderDetail.getAmount());//一种菜品的价格，累加
//存入订单详情表中
            orderDetailDao.insert(orderDetail);
        }
        System.out.println("amount = " + amount);
        // 5、保存订单数据，有许多数据需要单独设置
        orders.setNumber(String.valueOf(orderNumber));  //订单号
        orders.setAmount(amount);   //订单总价
        orders.setOrderTime(LocalDateTime.now());   //下单时间
        orders.setCheckoutTime(LocalDateTime.now());    //结账时间
        orders.setUserId(userId);
        orders.setConsignee(addressBook.getConsignee());    //设置收件人姓名
        orders.setUserName(user.getName()); //设置用户名
        orders.setPhone(user.getPhone());   //设置手机号
        orders.setStatus(2);//1待付款，2待派送，3已派送，4已完成，5已取消
        orders.setAddress(
                (addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                        + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                        + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                        + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));//省+市+区域+详细地址
        orderDao.insert(orders);
        // 6、删除当前用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartDao.delete(wrapper);
        return "清空购物车成功";
    }
}
